package com.chuhe.starry.service.impl;

import com.chuhe.common.domain.entity.UserInfo;
import com.chuhe.common.domain.entity.UserInfoBeauty;
import com.chuhe.common.domain.enums.BeautyAccountStatusEnum;
import com.chuhe.common.domain.enums.PageSize;
import com.chuhe.common.domain.query.PaginationResultVO;
import com.chuhe.common.domain.query.SimplePage;
import com.chuhe.common.domain.vo.req.AdminAddBeautyAccountReq;
import com.chuhe.common.domain.vo.req.AdminLoadBeautyAccountReq;
import com.chuhe.common.exception.BusinessException;
import com.chuhe.common.exception.CommonErrorEnum;
import com.chuhe.starry.dao.UserInfoBeautyDao;
import com.chuhe.starry.dao.UserInfoDao;
import com.chuhe.starry.service.UserInfoBeautyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 楚河
 * @Date: 2025年05月11日12:04
 */
@Service
public class UserInfoBeautyServiceImpl implements UserInfoBeautyService {
    @Autowired
    private UserInfoBeautyDao userInfoBeautyDao;
    @Autowired
    private UserInfoDao userInfoDao;

    /**
     * 查询所有靓号
     * @Author: 楚河
     * @Date: 2025-05-18 11:10
     */
    @Override
    public PaginationResultVO<UserInfoBeauty> loadBeautyAccountList(AdminLoadBeautyAccountReq req) {
        int count = userInfoBeautyDao.selectCount();
        int pageSize = req.getPageSize() == null ? PageSize.SIZE15.getSize() : req.getPageSize();

        SimplePage page = new SimplePage(req.getPageNo(), count, pageSize);
        req.setSimplePage(page);
        List<UserInfoBeauty> list = userInfoBeautyDao.selectUserInfoBeautyAccountList(req);
        PaginationResultVO<UserInfoBeauty> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增靓号
     * @Author: 楚河
     * @Date: 2025-05-18 11:22
     */
    @Override
    public void saveBeautyAccount(AdminAddBeautyAccountReq beauty) {
        if (beauty.getId() != null) {
            UserInfoBeauty dbInfo = userInfoBeautyDao.selectById(beauty.getId());
            if (BeautyAccountStatusEnum.USEED.getStatus().equals(dbInfo.getStatus())) {
                //已经使用的不允许修改
                throw new BusinessException(CommonErrorEnum.CODE_600);
            }
        }
        //判断邮箱是否已经存在
        UserInfoBeauty dbInfo = userInfoBeautyDao.selectByEmail(beauty.getEmail());
        if (beauty.getId() == null && dbInfo != null) {
            throw new BusinessException("靓号邮箱已经存在");
        }

        if (beauty.getId() != null && dbInfo != null && dbInfo.getId() != null && !beauty.getId().equals(dbInfo.getId())) {
            throw new BusinessException("靓号邮箱已经存在");
        }

        //判断靓号是否存在
        dbInfo = userInfoBeautyDao.selectByUserId(beauty.getUserId());
        if (beauty.getId() == null && dbInfo != null) {
            throw new BusinessException("靓号已经存在");
        }

        if (beauty.getId() != null && dbInfo != null && dbInfo.getId() != null && !beauty.getId().equals(dbInfo.getId())) {
            throw new BusinessException("靓号已经存在");
        }

        //判断邮箱是否已经注册
        UserInfo userInfo = userInfoDao.selectByEmail(beauty.getEmail());
//        if (userInfo != null) {
//            throw new BusinessException("靓号邮箱已经被注册");
//        }

        userInfo = userInfoDao.selectByUserId(beauty.getUserId());
        if (userInfo != null) {
            throw new BusinessException("靓号已经被注册");
        }

        if (beauty.getId() != null) {
            userInfoBeautyDao.updateBeautyById(beauty, beauty.getId());
        } else {
            userInfoBeautyDao.saves(beauty);
        }
    }

    /**
     * 删除靓号
     * @Author: 楚河
     * @Date: 2025-05-18 11:40
     */
    @Override
    public Object delBeautAccount(Integer id) {
        userInfoBeautyDao.delBeautAccount(id);
        return null;
    }
}
